import logging
import random

from myfirstproject.lib.base import *

log = logging.getLogger(__name__)

class OrderController(BaseController):
    
    def index(self):
        """
            Initialize variables for each request, to random images and clean session
        """
        
        #clean random images
        session['randomImages'] = ''
        
        #clean selected images
        session['imagesSelectedOk'] = ''
        session.save()
        
        c.randomImages = g.games.randomImages()
        
        #render the index page
        return render('/index.mako')        
        
    def addImageWarcraft(self):
        """
            This is the action for the event when a image is droped into warcraft area
        """

        game = 'Warcraft3'
        return self.addImage(game)

    def addImageDiablo(self):
        """
            This is the action for the event when a image is droped into diablo area
        """

        game = 'Diablo2'
        return self.addImage(game)

    def addImage(self, game):
        """
            Update the list of selected images and render the status message
        """

        #get the url of the selected image
        imageSelected = self.getImageSelected()

        #checks if selected image is belong to the game
        ok = self.checkImage(game, imageSelected)

        #indicate if all images was ordered
        finished = False

        #if the image was order corrected, the list of selected images needs to be updated
        if ok:
            # if the images is already in a selected list, there is nothing to be done
            if session['imagesSelectedOk'].find(imageSelected) < 0:
                newList = []
                if session['imagesSelectedOk'] :
                    newList = session['imagesSelectedOk'].split(';')
                newList.append(imageSelected)
                session['imagesSelectedOk'] = ';'.join(newList)
                session.save()
        #if the image was not selected corrected, the list needs to be updated
        else:
            # remove the selected image from the list
            if session['imagesSelectedOk'].find(imageSelected) >= 0:
                newList = session['imagesSelectedOk'].split(';')
                newList.remove(imageSelected)
                session['imagesSelectedOk'] = ';'.join(newList)
                session.save()
        #all images was selected corrected
        if len(session['imagesSelectedOk'].split(';')) >= len(session['randomImages'].split(';')):
            finished = True
        #updates status message
        return render('/addImage.mako', imageSelectedOk=ok, game=game, finished=finished)

    def getImageSelected(self):
        """
            Return the url of the selected image
        """

        i = int(request.params['id'].split('_')[1])
        randomImages = session['randomImages'].split(';')
        imageSelected = randomImages[i]
        return imageSelected

    def checkImage(self, game, imageSelected):
        """
            Check if the image belongs to the game
        """

        if g.games.geImageGame(imageSelected) == game:
            return True
        return False
